package cn.lls.controller.employee;

import cn.lls.base.result.R;
import cn.lls.excel.WageExcel;
import cn.lls.req.emp.ConditionSearchWageExcelReq;
import cn.lls.req.emp.ConditionSearchWageReq;
import cn.lls.req.emp.PersonWageReq;
import cn.lls.req.emp.WageReq;
import cn.lls.service.IWageService;
import cn.lls.vo.emp.WageInfoVO;
import com.alibaba.excel.EasyExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

/**
 * 员工工资操作控制器
 *
 * @ClassName WageOpeController
 * @Author 划水艺术家
 * @Date 2022/11/22 14:33
 * @Version 1.0
 **/

@RestController
@RequestMapping("/emp/wage")
@Slf4j
@Api(tags = "员工工资信息操作控制器")
public class WageOpeController {

    @Autowired
    private IWageService wageService;


    @GetMapping("/{wageId}")
    @ApiOperation(value = "根据Id查询工资详情信息", notes = "根据Id查询工资详情信息", response = R.class, httpMethod = "GET")
    public R getEmployeeById(@PathVariable("wageId") @NotNull(message = "传参缺失") Long wageId){
        return R.success(wageService.getWageById(wageId));
    }

    @PostMapping("/peresonwage")
    public R getEmployeeByEmpId(@RequestBody PersonWageReq personWageReq){
        WageInfoVO wageInfoVO = new WageInfoVO();
        BeanUtils.copyProperties(wageService.getWageByEmpId(personWageReq),
                wageInfoVO);
        return R.success(wageInfoVO);
    }


    @PostMapping("/info")
    @ApiOperation(value = "分页条件查询员工工资", notes = "分页条件查询员工工资", response = R.class, httpMethod = "POST")
    public R getEmployeeByConditionAndPage(@RequestBody @Validated ConditionSearchWageReq conditionSearchWageReq,
                                           BindingResult bindingResult){
        System.out.println("condiWage = " + conditionSearchWageReq);
        WageInfoVO wageInfoVO = new WageInfoVO();
        BeanUtils.copyProperties(wageService.getWageInfoByPageAndCondition(conditionSearchWageReq),
                wageInfoVO);
        return R.success(wageInfoVO);
    }



    @PostMapping
    @ApiOperation(value = "员工工资录入", notes = "员工工资录入", response = R.class, httpMethod = "POST")
    public R enterWage(@RequestBody @Validated WageReq wageReq,
                       BindingResult bindingResult){

        wageService.saveWage(wageReq);
        return R.success();
    }

    @PutMapping
    @ApiOperation(value = "更改员工工资", notes = "更改员工工资", response = R.class, httpMethod = "PUT")
    public R updateWage(@RequestBody @Validated WageReq wageReq,
                            BindingResult bindingResult){
        wageService.updateWage(wageReq);
        return R.success();
    }

    @DeleteMapping("/{wageId}")
    @ApiOperation(value = "删除员工工资", notes = "删除员工工资", response = R.class, httpMethod = "DELETE")
    public R delWage(@PathVariable("wageId") @NotNull(message = "匿名工资主键，无法删除") Long wageId,
                         BindingResult bindingResult){
        wageService.removeById(wageId);
        return R.success();
    }

    @PostMapping("/info/export")
    @ApiOperation(value = "导出工资信息到excel", notes = "导出工资信息到excel", response = R.class, httpMethod = "POST", produces = "application/octet-stream")
    public void exportEmployeeInfo(@RequestBody @Validated ConditionSearchWageExcelReq conditionSearchWageReq,
                                   BindingResult bindingResult, HttpServletResponse response) throws IOException {
        try {
            // 设置文本内省
            response.setContentType("application/vnd.ms-excel");
            // 设置字符编码
            response.setCharacterEncoding("utf-8");
            // 设置响应头
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition, lls-code");
            response.setHeader("Content-disposition", "attachment;filename=wageInfo.xlsx");
            response.setHeader("lls-code", "200");
            List<WageExcel> wageExcel = wageService.getWageExcel(conditionSearchWageReq);
            log.info("wageExcel = {}", wageExcel);
            EasyExcel.write(response.getOutputStream(), WageExcel.class)
                    .sheet("薪资信息")
                    .doWrite(wageExcel);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
